################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		April 9, 2017    
#
################################
# code edits that produced by Chen & Rodden (2013), to replicate their 2000 analysis 
#  for competitiveness outcomes                                                 
################################
# calls sims2000.R to load data for plotting

rm(list=ls())    

GetColorHexAndDecimal <- function(color)
{
  c <- col2rgb(color)
  sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3])
}

library(stringr)  
library(foreign)

setwd("~/Dropbox/StateRedistricting/replication/short/")
source('sims2000.R')

#########################
# cd vertical
#########################

{
pdf(width=5,file='cd_2000-vert.pdf')
plot(x=-1000, y=-1000, type='n', xlim=c(-.09, .5), ylim=c(.5,20), axes=F, ylab="", xlab="")#, main=paste("Margins of Victory in House Elections\n",state,sep=""))
axis(1,at=c(0,.1,.2,.3,.4,.5),labels=c(0,.1,.2,.3,.4,.5))
#cnts=0
cnts=20 

#legend(cex=.7,y=cnts-16.018,x=.325,border=F,box.lty=1,legend=c('Enacted Plan','Simulated Plan'),	
# col=c('red',gray(.65)),lwd=c(1,1),pch=c(1,19),lty=c(NA,NA))
pcc=17
#Means and Medians
for (state in states_ix){
	cnts=cnts-1
	state.dir <- paste(charts.dir,state,sep="/") 
	temp.sims <- t(sims[sims$st==state,-c(1:3)])
	temp.00 <- elections00[elections00$st==state, -c(1:3)]
	temp.02 <- elections02[elections02$st==state, -c(1:3)] 
	
	#make absolute margin of victory
	temp.sims <- abs(temp.sims)
	temp.00 <- abs(temp.00)
	temp.02 <- abs(temp.02)

	#Means :: average over simulatons, i.e., counterfactual districtings
	means.sim <- apply(temp.sims, 1, mean)
	#moments.sim <- quantile(means.sim,prob=c(.025,.975))
	#mean.00 <- mean(temp.00)
	mean.02 <- mean(temp.02)
	#hypothesis-test-like statistics
	mu=mean(means.sim,na.rm=T)
	sds=sd(means.sim,na.rm=T) 
	s.means.sim=((means.sim-mu)/sds)
	s.mean.02=((mean.02-mu)/sds)	
	s.dif=mean(s.mean.02-s.means.sim)
	if(s.dif>0){
		p.dif=sum(s.mean.02<s.means.sim)/length(s.means.sim)    
	} else{                                                     
		p.dif=sum(s.mean.02>s.means.sim)/length(s.means.sim)    
	}	  
	
	cols='red' 
	ltys=2
	if(p.dif<.05){
		cols='red' 
		ltys=1
	}
	 	 
	points(x=means.sim, y=jitter(factor=.2,rep(cnts,length(means.sim))), col=gray(.65), lwd=1,pch=pcc,cex=.52)
#	points(y=cnts, x=mean.02, col='red', pch=1,cex=.85) 
	points(y=cnts, x=mean.02, col=cols, pch=1,cex=sqrt(1+abs(s.dif))/1.25,lty=2) 	
	
	#text(x=-.0,y=cnts,labels=as.character(state),srt = 1, pos = 2,cex=.65)	
	#text(adj=c(0,NA),x=-.0,y=cnts,labels=paste(as.character(state),' (',str_sub(s.dif,1,str_locate(s.dif,pattern='[/.]')[1]+2),')',sep=''),srt = 1, pos = 2,cex=.5)		
	 
	text(adj=c(0,NA),x=-.055,y=cnts,labels=as.character(state),srt = 1, pos = 2,cex=.65)			
	text(col='black',adj=c(0,NA),x=.004,y=cnts,labels=paste('(',str_sub(s.dif,1,str_locate(s.dif,pattern='[/.]')[1]+2),')',sep=''),srt = 1, pos = 2,cex=.5)				

}         

# lines and legend graphics
lines(col='grey',lty=2,y=c(.5,.5),x=c(-.0515,.5))  
lines(col='grey',lty=2,y=c(7.5,7.5),x=c(-.0515,.5))
lines(col='grey',lty=2,y=c(11.5,11.5),x=c(-.0515,.5)) 
lines(col='grey',lty=2,y=c(15.5,15.5),x=c(-.0515,.5)) 
lines(col='grey',lty=2,y=c(19.5,19.5),x=c(-.0515,.5))                                                  

axis(2,line=-1.5,at=c(.5,7.5,11.5,15.5,19.5),labels=F,tck=-.02)

text(x=.5-.0105,y=19.,labels='Democratic',cex=.75,adj=c(1,1),srt=0)
text(x=.5-.0105,y=15.,labels='Republican',cex=.75,adj=c(1,1),srt=0)
text(x=.5-.0105,y=11.,labels='Bipartisan',cex=.75,adj=c(1,1),srt=0)
text(x=.5-.0105,y=7.,labels='Court/\nIndependent',cex=.75,adj=c(1,1),srt=0)
mtext("Mean Expected Margin of Victory",side=1,at=.25,adj=.5,line=3)
dev.off()
}   
 
#end  
